﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Database.Context;
using Database.Models;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Biz
{
    internal class PhuongTienBiz : BaseBiz
    {
        public PhuongTienBiz()
        {
            DbContext = new Context();
        }

        public PhuongTienBiz(Context context)
        {
            DbContext = context;
        }

        public override void Insert(BaseModel model)
        {
            var item = model as PhuongTien;
            item.Created = DateTime.Now;
            item.Modified = DateTime.Now;
            item.CreatedBy = Utilities.CurrentUser();
            item.ModifiedBy = Utilities.CurrentUser();

            DbContext.PhuongTiens.Add(item);
            DbContext.SaveChanges();
        }

        public override void Update(BaseModel model)
        {
            var item = model as PhuongTien;
            item.Modified = DateTime.Now;
            item.ModifiedBy = Utilities.CurrentUser();

            DbContext.Entry(item).State = (EntityState)System.Data.EntityState.Modified;
            DbContext.SaveChanges();
        }

        public override void Delete(BaseModel model)
        {
            DbContext.PhuongTiens.Attach(model as PhuongTien);
            DbContext.PhuongTiens.Remove(model as PhuongTien);
            DbContext.Entry(model).State = EntityState.Deleted;

            DbContext.SaveChanges();
        }

        public override void DeleteItems(List<BaseModel> models)
        {
            throw new NotImplementedException();
        }

        public override BaseModel GetItemById(int id)
        {
            var model = (from item in DbContext.PhuongTiens
                         where item.Id == id
                         select item).First();
            return model;
        }

        #region GetAll

        public List<PhuongTien> GetAll()
        {
            return DbContext.PhuongTiens.OrderBy(item => item.Ten).ToList();
        }

        #endregion
    }
}